package com.autonavi.yunda.yunji.common.utils.geo;

import com.autonavi.yunda.yunji.common.exception.AmapException;
import com.autonavi.yunda.yunji.common.exception.AmapExceptionCode;

/**
 * @author guohongyun
 * 地理坐标转换
 */
public class Gps84ChangeGcj02Util {

    public static double pi = 3.141592653589793D;
    public static double a = 6378245.0D;
    public static double ee = 0.006693421622965943D;


    public static String gps84ToGcj02(String gpsWkt) throws AmapException {
        String[] xYs = gpsWkt.split(",");
        int xyLength3 = 3;
        if (xYs.length < xyLength3) {
            throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM, "无效参数: typeInfo(wkt) 点个数不得小于3");
        }
        String s = "";
        for (String position : xYs) {
            String[] xY = position.split(" ");
            int xyLength2 = 2;
            if (xY.length != xyLength2) {
                throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM, "无效参数: typeInfo(wkt) 格式错误 " + xY);
            }
            s += Gps84ChangeGcj02Util.gps84ToGcj02(Double.parseDouble(xY[0]), Double.parseDouble(xY[1])) + ",";
        }
        String gcjWtk = s.substring(0, s.length() - 1);
        return gcjWtk;
    }


    public static String gps84ToGcj02(double lon, double lat) throws AmapException {
        if (outOfChina(lat, lon)) {
            throw new AmapException(AmapExceptionCode.COMMON_ILLEGAL_PARAM, "无效参数: typeInfo(wkt) 包含越界经纬度坐标");
        }
        double dLat = transformLat(lon - 105.0D, lat - 35.0D);
        double dLon = transformLon(lon - 105.0D, lat - 35.0D);
        double radLat = lat / 180.0D * pi;
        double magic = Math.sin(radLat);
        magic = 1.0D - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = dLat * 180.0D / (a * (1.0D - ee) / (magic * sqrtMagic) * pi);
        dLon = dLon * 180.0D / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return String.format("%.6f", mgLon) + " " + String.format("%.6f", mgLat);
    }

    public static boolean outOfChina(double lat, double lon) {
        double lonMin = 72.004000000000005D;
        double lonMax = 137.8347D;
        double latMin = 0.8293D;
        double latMax = 55.827100000000002D;

        if ((lon < lonMin) || (lon > lonMax)) {
            return true;
        }
        if ((lat < latMin) || (lat > latMax)) {
            return true;
        }
        return false;
    }

    public static double transformLat(double x, double y) {
        double ret = -100.0D + 2.0D * x + 3.0D * y + 0.2D * y * y + 0.1D * x * y +
                0.2D * Math.sqrt(Math.abs(x));
        ret += (20.0D * Math.sin(6.0D * x * pi) + 20.0D * Math.sin(2.0D * x * pi)) * 2.0D / 3.0D;
        ret += (20.0D * Math.sin(y * pi) + 40.0D * Math.sin(y / 3.0D * pi)) * 2.0D / 3.0D;
        ret += (160.0D * Math.sin(y / 12.0D * pi) + 320.0D * Math.sin(y * pi / 30.0D)) * 2.0D / 3.0D;
        return ret;
    }

    public static double transformLon(double x, double y) {
        double ret = 300.0D + x + 2.0D * y + 0.1D * x * x + 0.1D * x * y + 0.1D *
                Math.sqrt(Math.abs(x));
        ret += (20.0D * Math.sin(6.0D * x * pi) + 20.0D * Math.sin(2.0D * x * pi)) * 2.0D / 3.0D;
        ret += (20.0D * Math.sin(x * pi) + 40.0D * Math.sin(x / 3.0D * pi)) * 2.0D / 3.0D;

        ret = ret + (150.0D * Math.sin(x / 12.0D * pi) + 300.0D * Math.sin(x / 30.0D * pi)) * 2.0D / 3.0D;
        return ret;
    }
}
